Yacon.widget.TTab = function(conText){	
	
	Yacon.apply(this,conText);
	
	if(this.view){
		this.element = this.view;
		this.elementID = this.view.id;
		this.elementType = this.view.type;
	}
	
};

Yacon.widget.TTab.prototype = {
	
	/* 获取HTML */	
	getHtml: function(){
		
		var html = "";
		
		html += "<div ";	
		html += " class='row-ht-auto col' ";	
		html += " lily-id='"+this.elementID+"' lily-type='"+this.elementType+"' ";		
		html += ">"
		
		/* 获取头信息 */	
		html += this.getTabHtml();
		
		/* 获取体信息 */
		html += this.getContentHtml();
		
		html += "</div>";
		
		return html;
		
	},
	
	/* 获取头HTML */
	getTabHtml: function(){
		
		var html = "";
		
		html  += "<div id='"+this.elementID+"_tag' class='row-ht-fix'>";
		
		if(this.view.style == "pill"){
			html += "<ul id='"+this.elementID+"' class='nav nav-pills' role='tablist' >"
		}else{
			html += "<ul id='"+this.elementID+"' class='nav nav-tabs' role='tablist' >"
		}
			
		//拼标签
		this.tabs = this.view.tabs;
		for(var i=0;i<this.tabs.length;i++){
			
			var activeTab = this.tabs[i];
			
			if(Yacon.isEmpty(activeTab.menu)){
				
				html += "<li role='presentation'>";
				
				if(this.view.style == "pill"){
					html += "<a href='#"+activeTab.id+"' id='"+activeTab.id+"_tab' role='tab' data-toggle='pill' aria-controls='"+activeTab.id+"' aria-expanded='false'>";			
				}else{
					html += "<a href='#"+activeTab.id+"' id='"+activeTab.id+"_tab' role='tab' data-toggle='tab' aria-controls='"+activeTab.id+"' aria-expanded='false'>";			
				}
						
				html += activeTab.name;
				
				html += "</a>"
				html += "</li>";
				
			}
			else{
				
				html += "<li role='presentation' class='dropdown'>";			
				html += "<a href='#' id='"+activeTab.id+"_tab' class='dropdown-toggle' data-toggle='dropdown' aria-controls='"+activeTab.id+"-contents' aria-expanded='false'>"+activeTab.name+" <span class='caret'></span></a>";				
				html += "<ul class='dropdown-menu' role='menu' aria-labelledby='"+activeTab.id+"_tab' id='"+activeTab.id+"_tab-contents'>";
				
				var menus = activeTab.menu;
				for(var j=0;j<menus.length;j++){
					
					var activeMenu = menus[j];
					
					html += "<li class=''>";
					
					if(this.view.style == "pill"){
						html += "<a  href='#"+activeMenu.id+"'  tabindex='-1'  role='tab'  id='"+activeMenu.id+"_tab'  data-toggle='pill'  aria-controls='"+activeMenu.id+"'  aria-expanded='false'>";					
						
					}else{
						html += "<a  href='#"+activeMenu.id+"'  tabindex='-1'  role='tab'  id='"+activeMenu.id+"_tab'  data-toggle='tab'  aria-controls='"+activeMenu.id+"'  aria-expanded='false'>";					
					}
					
					html += activeMenu.name;
					html += "</a>";						
					html += "</li>";
				}
				
				
				html += "</ul>";
				html += "</li>";
				
			}
			
			
		}
		
		html += "</ul>"
		
		html  += "</div>";
		
		return html;
	},
	
	/* 获取体HTML */
	getContentHtml: function(){
		
		var html = "";
		
		html  += "<div id='"+this.elementID+"_content' class='row-ht-auto tab-content col lily-frame' >";
		
		for(var i=0;i<this.tabs.length;i++){
			
			var activeTab = this.tabs[i];
			
			if(Yacon.isEmpty(activeTab.menu)){
				html += "<div role='tabpanel' class='tab-pane fade col' style='height:100%;' id='"+activeTab.id+"' aria-labelledby='"+activeTab.id+"_tab'>";
				
				html += "</div>";
			}else{
				
				var menus = activeTab.menu;
				for(var j=0;j<menus.length;j++){
					
					var activeMenu = menus[j];
					html += "<div role='tabpanel' class='tab-pane fade col' style='height:100%;' id='"+activeMenu.id+"' aria-labelledby='"+activeMenu.id+"_tab'>";
					
					html += "</div>";					
					
				}
				
			}
			
			
		}
		
		html  += "</div>";
		
		return html;		
	},
	
	layoutBefore: function(){
		
	},
	
	getTag: function(tagID){
		
		if(Yacon.isString(tagID)){
			tagID = tagID.trim().toUpperCase();
			
			for(var i=0;i<this.tabs.length;i++){
				var activeTab = this.tabs[i];
				var activeId = activeTab.id.trim().toUpperCase();
				
				if(activeId == tagID){
					return activeTab;
				}
				
				if(!Yacon.isEmpty(activeTab.menu)){
					var menus = activeTab.menu;

					for(var j=0;j<menus.length;j++){
						
						var activeMenu = menus[j];
						
						var childId = activeMenu.id.trim().toUpperCase();
						
						if(childId == tagID){
							return activeMenu;
						}
						
					}
						
				}
				
			}
		}
		
		if(Yacon.isNumber(tagID)){
			if(this.tabs.length > tagID && tagID >= 0){
				return this.tabs[tagID];
			}
		}
		
		return null;
	},
	
	addEvents: function(){
		
		this.dataSet = new Yacon.data.DataSet(this.page);
		var self = this;
		var search = this.page.element.search + " div[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		
		//同步加载
		if(this.view.load && this.view.load == "asyn"){
			
			//隐藏绑定方法
			$(search+' a[role="tab"]').on('hide.bs.tab', function (e) {
			    var tagID = $(this).attr("aria-controls");
			    $(search +" #"+ tagID).empty();
			});
			
			//展示绑定方法
			$(search+' a[role="tab"]').on('show.bs.tab', function (e) {
				
				var tagID = $(this).attr("aria-controls");
				
				$(search +" #"+ tagID).empty();
					
				var tagObj = self.getTag(tagID);
				var tagUrl = tagObj.url;
				
				if(Yacon.isEmpty(tagUrl)){
					return;
				}
						
				if(tagUrl.startsWith("/UI")){
					tagUrl = self.page.element.contextPath + tagUrl;
				}
				
				self.dataSet.switchPage(tagUrl);
				
				if(!Yacon.isEmpty(self.dataSet.rsp)){
					/* JS填充 */
					$(search +" #"+ tagID).append(self.dataSet.rsp);
					
					self.tagPage = window.page;
					self.tagPage.tags = self;
					
					var pageHtml = self.tagPage.getHtml();
					
					$(search +" #"+ tagID).append(pageHtml);
				}
				
			});
			
			$(search+' a[role="tab"]').on('shown.bs.tab', function (e) {
				self.tagPage.layout();		
				self.tagPage.addEvents();
			});
			
			this.clickTag(this.view.active);
			
		}
		
		else {
					
			var tags = $(search+' a[role="tab"]');
			
			for(var i=0;i<tags.length;i++){
				
				var tagID = tags.eq(i).attr("aria-controls");
				
				$(search +" #"+ tagID).empty();
					
				var tagObj = self.getTag(tagID);
				var tagUrl = tagObj.url;
				
				if(Yacon.isEmpty(tagUrl)){
					return;
				}
						
				if(tagUrl.startsWith("/UI")){
					tagUrl = self.page.element.contextPath + tagUrl;
				}
				
				self.dataSet.switchPage(tagUrl);
				
				if(!Yacon.isEmpty(self.dataSet.rsp)){
					/* JS填充 */
					$(search +" #"+ tagID).append(self.dataSet.rsp);
					
					self.tagPage = window.page;
					self.tagPage.tags = self;
					
					var pageHtml = self.tagPage.getHtml();
					
					$(search +" #"+ tagID).append(pageHtml);
					
					self.tagPage.addEvents();
				}
				
			}
			
			$(search+' a[role="tab"]').on('show.bs.tab', function (e) {
				self.tagPage.layout();					
			});
			
			
			this.clickTag(this.view.active);
			
		}
		
		$('.nav-pills, .nav-tabs').tabdrop();

		
	},
	
	/**
	 * 点击标签
	 * @param tag
	 */
	clickTag: function(tag){
		
		var tagObj = this.getTag(tag);
		
		if(!Yacon.isEmpty(tagObj)){
			var tagID = tagObj.id;
			var search = this.page.element.search + " div[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
			$(search+" a[role='tab'][aria-controls='"+tagID+"']").tab('show'); 
		}
		
		
	}
	
};